//
//  RegionPickerTool.m
//  PickerView
//
//  Created by  zengchunjun on 2017/4/20.
//  Copyright © 2017年  zengchunjun. All rights reserved.
//

#import "RegionPickerTool.h"
#import "NYRegionListModel.h"
@interface RegionPickerTool ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIPickerView *provincePicker;

@property (nonatomic,assign) NSString *pickRegion;

@property (nonatomic, assign) NSInteger provinceIndex,citieIndex;

@end

@implementation RegionPickerTool

- (instancetype)initWithFrame:(CGRect)frame andProvinceArray:(NSMutableArray *)province andCityArray:(NSMutableArray *)city {
    if (self = [super initWithFrame:frame]) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"RegionPickerTool" owner:nil options:nil]firstObject];
    }
    self.frame = frame;
    self.cityArray = city;
    self.provinceArray = province;
    [self.provincePicker selectRow:0 inComponent:0 animated:0];
    [self.provincePicker selectRow:0 inComponent:1 animated:0];
    return self;
}

- (IBAction)pickDone:(UIButton *)sender {
    RegionListModel *pro = self.provinceArray[_provinceIndex];
    RegionListModel *city = self.cityArray[_citieIndex];
    self.confirmBlock([NSString stringWithFormat:@"%@-%@",pro.name,city.name]);
}


- (IBAction)pickCancel:(UIButton *)sender {
    self.confirmBlock(nil);
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component==0) {
        return self.provinceArray.count;
    } else {
        return self.cityArray.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component==0) {
        RegionListModel *model = self.provinceArray[row];
        return model.name;
    } else {
        RegionListModel *model = self.cityArray[row];
        return model.name;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component==0) {
        self.provinceIndex = row;
        self.citieIndex = 0;
        RegionListModel *model = self.provinceArray[row];
        self.provinceBlock(model.regionid);
    } else {
        self.citieIndex = row;
    }
}

-(void)setCityArray:(NSMutableArray *)cityArray{
    _cityArray = cityArray;
    [self.provincePicker reloadComponent:1];
}

-(void)setProvinceArray:(NSMutableArray *)provinceArray{
    _provinceArray = provinceArray;
    [self.provincePicker reloadComponent:0];
    
}

@end
